package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.LogService;
import csu.web.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class OrderServlet extends HttpServlet {
    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/viewOrder.jsp";

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();

        String cardType = req.getParameter("cardType");
        String creditCard = req.getParameter("creditCard");
        String expiryDate = req.getParameter("expiryDate");
        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        String address1 = req.getParameter("address1");
        String address2 = req.getParameter("address2");
        String city = req.getParameter("city");
        String state = req.getParameter("state");
        String zip = req.getParameter("zip");
        String country = req.getParameter("country");

        // 修改订单消息
        Order order = (Order)session.getAttribute("order");
        order.setCardType(cardType);
        order.setCreditCard(creditCard);
        order.setExpiryDate(expiryDate);
        order.setBillToFirstName(firstName);
        order.setBillToLastName(lastName);
        order.setShipToFirstName(firstName);
        order.setShipToLastName(lastName);
        order.setBillAddress1(address1);
        order.setBillAddress2(address2);
        order.setShipAddress1(address1);
        order.setShipAddress2(address2);
        order.setBillCity(city);
        order.setShipCity(city);
        order.setBillState(state);
        order.setShipState(state);
        order.setBillZip(zip);
        order.setShipZip(zip);
        order.setBillCountry(country);
        order.setShipCountry(country);

        session.setAttribute("order",order);


        //Order order = (Order) session.getAttribute("order");
        session.setAttribute("lineItems",order.getLineItems());

        OrderService orderService = new OrderService();
        orderService.insertOrder(order);
        int orderId = 100;
        // 重置购物车
        Cart cart = new Cart();
        session.setAttribute("cart",cart);

        Account account = (Account) session.getAttribute("account");
        if(account != null) {
            String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") +"用户" +account.getUsername()+ "  " + "订单完成核对页面[订单号："+orderId+"]";
            logService.insertLogInfo(account.getUsername(),logInfo);
        }

        req.getRequestDispatcher(VIEW_ORDER).forward(req,resp);
    }
}
